const express = require('express'),
  router = express.Router(),
  util = require("../util/util"),
  statistic = require("../model/statistic"),
  reObj = util.reObj;


/**
 * @function 添加支付信息
 * @author Eric
 * @date 2018-08-01
 * @param name,code,addr,area,status
 */

router.post('/addstatistic', (req, res) => {
  req.body.userip=req.ip.split("f:")[1]
    statistic.create(        
        req.body
    ).then(record => {      
        if (record) {
            res.send(reObj(0, '添加成功', record[0]))
        }
    }).catch(err => {
        res.send(reObj(-1, '添加失败', err))
    });

})
/**
 * @function 编辑支付信息
 * @author Eric
 * @date 2018-08-01
 * @param id,name,code,addr,area,status
 */
router.post('/update', (req, res) => {
  let id = req.body.id
  let params = {
    id: id
  }

    statistic.update(req.body, {where: params}).then(record => {
        if (record[0] > 0) {
            console.log("record:"+JSON.stringify(record))
            res.send(reObj(0, '修改成功', record[0]))
        } else {
            res.send(reObj(-1, '无效的id', []))
        }
    }).catch(err => {
        res.send(reObj(-1, '修改失败', err))
    });

})

/**
 * @function 删除信息
 * @author Eric
 * @date 2018-07-31
 * @param id
 */
router.post('/del', (req, res) => {
  let params = {
    id: req.body.id
  }

  statistic.destroy({where: params}).then(record => {
    if (record > 0) {
      res.send(reObj(0, '删除会议成功', record))
    } else {
      res.send(reObj(-1, '删除会议失败或此记录不存在', record))
    }
  }).catch(err => {
    res.send(reObj(-1, '删除会议失败', err))
  });
})

/**
 * @function 获取单个信息
 * @author Eric
 * @date 2018-08-01
 * @param meetingid
 */
router.post('/findone', (req, res) => {
  let params = {}
  let id = req.body.id
  if (id) {
    params.id = id;

    statistic.findOne({where: params}).then(record => {
      if (record) {
        res.send(reObj(0, '获取信息成功', record))
      } else {
        res.send(reObj(-1, '获取信息失败或此记录不存在', {}))
      }
    }).catch(err => {
      res.send(reObj(-1, '获取信息失败', err))
    });
  } else {
    res.send(reObj(-1, 'id不能为空', []))
  }
})


/**
 * @function 获取所有数据(支持分页，条件过滤)
 * @author Eric
 * @date 2018-07-31
 * @param page,pagerow,meetingname,meetingtype
 */

router.post('/findall', (req, res) => {
  /* let meetingname = req.body.meetingname || ''
   let meetingtype = req.body.meetingtype || ''*/
   let params = req.body
   let pagerow = parseInt(req.body.pagerow)
   let page = parseInt(req.body.page)
   if(pagerow){
     delete params.pagerow;
   }
   if(page){
     delete params.page;
   }
   //对页码进行容错
   if (pagerow > 999) {
     pagerow = 999
   }
   if (page == 0) {
     page = 1
   }
   let sort="creattime DESC"
    if(req.body.top==="0")
    {
      sort="pvtotal DESC"
    }
    else if(req.body.top==="1")
    {
      sort="paymentcount DESC"
    }
    else if(req.body.top==="2")
    {
      sort="amounttotal DESC"
    }
    else
    {
      sort="creattime DESC"
    }

 
   //筛选条件
 /*  if (meetingname) {
     params.meetingname = {'$like': '%' + meetingname + '%'}
   }
   if (meetingtype) {
     params.meetingtype = meetingtype
   }*/
   if(req.body.startDate&&req.body.endDate)
   {
    params.creattime={$between: [req.body.startDate,req.body.endDate]}
   }
   console.log("params:"+JSON.stringify(params))
   delete params["startDate"]
   delete params["endDate"]
   delete params["top"]
   delete params["page"]
    delete params["pagerow"]
   statistic.findAndCountAll({
     order: sort,
     limit: pagerow || 100,
     offset: parseInt(pagerow * (page - 1)) || 0,
     where: params
   }).then(record => {
     if(record.count>0){
       res.send(reObj(0, '获取数据成功', record.rows, record.count))
     }
     else
     {
       res.send(reObj(0, '没有符合条件的记录', record.rows, record.count))
     }
     
   }).catch(err => {
     res.send(reObj(-1, '获取数据失败', err))
   });
 })
 
 router.post('/sumall', (req, res) => {
  /* let meetingname = req.body.meetingname || ''
   let meetingtype = req.body.meetingtype || ''*/
   let params = req.body
   let pagerow = 100000//parseInt(req.body.pagerow)
   let page = 1//parseInt(req.body.page)
   if(pagerow){
     delete params.pagerow;
   }
   if(page){
     delete params.page;
   }
   //对页码进行容错
   if (pagerow > 999) {
     pagerow = 99999
   }
   if (page == 0) {
     page = 1
   }
   let sort="creattime DESC"
    if(req.body.top==="0")
    {
      sort="pvtotal DESC"
    }
    else if(req.body.top==="1")
    {
      sort="paymentcount DESC"
    }
    else if(req.body.top==="2")
    {
      sort="amounttotal DESC"
    }
    else if(req.body.top==="3")
    {
      sort="averageratio DESC"
    }
    else
    {
      sort="creattime DESC"
    }

 
   //筛选条件
 /*  if (meetingname) {
     params.meetingname = {'$like': '%' + meetingname + '%'}
   }
   if (meetingtype) {
     params.meetingtype = meetingtype
   }*/
   if(req.body.startDate&&req.body.endDate)
   {
    params.creattime={$between: [req.body.startDate,req.body.endDate]}
   }
   console.log("params:"+JSON.stringify(params))
   delete params["startDate"]
   delete params["endDate"]
   delete params["top"]
   delete params["page"]
    delete params["pagerow"]
   statistic.findAndCountAll({
     order: sort,
     limit: pagerow || 1000,
     offset: parseInt(pagerow * (page - 1)) || 0,
     where: params
   }).then(record => {
     if(record.count>0){
       let sumObj={amounttotal:0,pvtotal:0,paymentcount:0,averageratio:0}
      //console.log("item:"+JSON.stringify(record))
      record.rows.forEach(item=>{
        sumObj.amounttotal+=item.amounttotal
        sumObj.pvtotal+=item.pvtotal
        sumObj.paymentcount+=item.paymentcount
        sumObj.averageratio+=item.averageratio
       
      })
     // console.log("sumObj:"+JSON.stringify(sumObj))
      res.send(reObj(0, '获取信息成功', [sumObj]))
       //res.send(reObj(0, '获取数据成功', record.rows, record.count))
     }
     else
     {
       res.send(reObj(0, '没有符合条件的记录', record.rows, record.count))
     }
     
   }).catch(err => {
     console.log('err:'+err)
     res.send(reObj(-1, '获取数据失败', err))
   });
 })


 router.post('/top', (req, res) => {
  /* let meetingname = req.body.meetingname || ''
   let meetingtype = req.body.meetingtype || ''*/
   let params = req.body
   let pagerow = parseInt(req.body.pagerow)
   let page = parseInt(req.body.page)
   
   if(pagerow){
     delete params.pagerow;
   }
   if(page){
     delete params.page;
   }
   //对页码进行容错
   if (pagerow > 999) {
     pagerow = 999
   }
   if (page == 0) {
     page = 1
   }
 
   let sort="creattime DESC"
   if(req.body.top==="0")
   {
     sort="pvtotal DESC"
   }
   else if(req.body.top==="1")
   {
     sort="paymentcount DESC"
   }
   else if(req.body.top==="2")
   {
     sort="amounttotal DESC"
   }
   else
   {
     sort="creattime DESC"
   }
   //筛选条件
 /*  if (meetingname) {
     params.meetingname = {'$like': '%' + meetingname + '%'}
   }
   if (meetingtype) {
     params.meetingtype = meetingtype
   }*/
   if(req.body.startDate&&req.body.endDate)
   {
   // params.creattime={$between: [dayjs(req.body.startDate).format(), dayjs(req.body.endDate).format()]}
    params.creattime={$between: [req.body.startDate,req.body.endDate]}
   }
   
   delete params["startDate"]
   delete params["endDate"]
   delete params["top"]
   delete params["page"]
    delete params["pagerow"]
   statistic.findAndCountAll({
     order:sort,//"creattime DESC",
     limit: pagerow || 100,
     offset: parseInt(pagerow * (page - 1)) || 0,
     where: params
   }).then(record => {
     if(record.count>0){
       let rows=JSON.parse(JSON.stringify(record.rows))
       rows.forEach((item, index) => {
         item.order=index+1         
       })
       res.send(reObj(0, '获取数据成功', rows, record.count))
     }
     else
     {
       res.send(reObj(0, '没有符合条件的记录', record.rows, record.count))
     }
     
   }).catch(err => {
     res.send(reObj(-1, '获取数据失败', err))
   });
 })

module.exports = router;
